package com.qf.controller;

import com.qf.service.DtsGoodsService;
import com.qf.service.DtsOrderService;
import com.qf.service.DtsUserService;
import com.qf.util.ResponseUtil;
import com.qf.vo.StatGoods;
import com.qf.vo.StatOrder;
import com.qf.vo.StatVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/admin/stat")
public class DtsAdminStatController {
    @Autowired
    DtsUserService userService;
    @Autowired
    DtsOrderService orderService;
    @Autowired
    DtsGoodsService goodsService;
    @GetMapping("/user")
    public Object user() {
        // 获取用户统计信息
        List<Map> maps = userService.findAllUserDayStatis();
        // map的key是Columns需要的数据
        Set<Set> collect = maps.stream().map(s -> {
            return s.keySet();
        }).collect(Collectors.toSet());
        String[] strings = new String[0];
        for (Set set : collect) {
            Object[] objects = set.toArray();
            strings = Arrays.asList(objects).toArray(new String[set.size()]);
        }
        // 封装数据
        StatVo statVo = new StatVo();
        statVo.setColumns(strings);
        statVo.setRows(maps);
        return ResponseUtil.ok(statVo);
    }
    @GetMapping("/order")
    public Object order() {
        // 获取订单统计信息
        List<StatOrder> userOrderList = orderService.findAllOrderStatis();
        List<String> list = new ArrayList<>();
        list.add("day");
        list.add("orders");
        list.add("customers");
        list.add("amount");
        list.add("pcr");
        // 封装数据
        Map<String, Object> data = new HashMap<>();
        data.put("columns", list);
        data.put("rows", userOrderList);

        return ResponseUtil.ok(data);
    }
    @GetMapping("/goods")
    public Object goods() {
        // 获取商品统计信息
        List<StatGoods> userOrderList = goodsService.findAllGoodsStatis();
        List<String> list = new ArrayList<>();
        list.add("day");
        list.add("orders");
        list.add("products");
        list.add("amount");
        // 封装数据
        Map<String, Object> data = new HashMap<>();
        data.put("columns", list);
        data.put("rows", userOrderList);
        return ResponseUtil.ok(data);
    }

}